View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.impl; 20 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.List; 24 25 import net.sourceforge.mflow.api.MsgFlowComponent; 26 import net.sourceforge.mflow.api.MsgSource; 27 28 /*** 29 * Contains various algorithms for MsgFlowComponents 30 * 31 * @author <a href="mailto:david@carr.name">David Carr</a> 32 * @version $Revision: 1.4 $ 33 */ 34 class Algorithm { 35 private Algorithm() { 36 //prevent external construction 37 } 38 39 private static class ReceiverContainer { 40 private List receivers; 41 private boolean isDone = false; 42 43 ReceiverContainer(MsgFlowComponent comp) { 44 if (comp instanceof MsgSource) { 45 MsgSource src = (MsgSource) comp; 46 this.receivers = new ArrayList(Arrays.asList(src.getReceivers())); 47 } else { 48 this.receivers = new ArrayList(); 49 } 50 } 51 52 void remove(MsgFlowComponent c) { 53 this.receivers.remove(c); 54 } 55 56 int size() { 57 if (this.isDone) { 58 return -1; 59 } else { 60 return this.receivers.size(); 61 } 62 } 63 64 void done() { 65 this.isDone = true; 66 } 67 } 68 69 /*** 70 * Returns a new array of components containing the original components, 71 * sorted. The passed array should not contain any circuits, and all 72 * receivers should be included in the array 73 * 74 * @param components the components to sort 75 * @return a sorted array of the same components, or null if passed an 76 * invalid array 77 * @todo ensure that all child components are within the array 78 */ 79 static MsgFlowComponent[] sortComponents(MsgFlowComponent[] components) { 80 if (components == null) 81 return null; 82 ReceiverContainer[] receivers = new ReceiverContainer[components.length]; 83 for (int i = 0; i < components.length; i++) { 84 receivers[i] = new ReceiverContainer(components[i]); 85 } 86 MsgFlowComponent[] sorted = new MsgFlowComponent[components.length]; 87 for (int index = sorted.length - 1; index >= 0; index--) { 88 int i; 89 for (i = 0; i < receivers.length; i++) { 90 if (receivers[i].size() == 0) { 91 sorted[index] = components[i]; 92 for (int j = 0; j < components.length; j++) { 93 receivers[j].remove(components[i]); 94 //will be ignored if not applicable 95 } 96 receivers[i].done(); 97 break; //skip out of this loop to continue on the next 98 } 99 } 100 if (i == receivers.length) { 101 return null; //must be a component not in the array 102 } 103 } 104 return sorted; 105 } 106 }

This page was automatically generated by Maven